import pytest
from common.request_util_test import RequestUtil
from common.test_yaml_util_test import YamlUtil
from config import datas


class Testdaemonset:
    url_unify = "/clusters/host"

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.daemonset
    @pytest.mark.parametrize('daemonsetinfo',
                             YamlUtil().read_testcase_yaml('v25.06/resourcemanage/daemonset/1_get_daemonset_list.yaml'))
    def test_get_daemonset_list(self, daemonsetinfo):
        # 获取daemonset列表接口-正常
        base_url = datas.baseUrl + Testdaemonset.url_unify
        url = base_url + daemonsetinfo['request']['url'].replace('{namespace}', 'default')
        method = daemonsetinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == daemonsetinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.daemonset
    @pytest.mark.parametrize('daemonsetinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/daemonset/1_get_daemonset_list_exception.yaml'))
    def test_get_daemonset_list_exception(self, daemonsetinfo):
        # 获取daemonset列表接口-异常
        base_url = datas.baseUrl + Testdaemonset.url_unify
        url = base_url + daemonsetinfo['request']['url'].replace('{namespace}', 'default')
        method = daemonsetinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == daemonsetinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.daemonset
    @pytest.mark.parametrize('daemonsetinfo',
                             YamlUtil().read_testcase_yaml('v25.06/resourcemanage/daemonset/2_create_daemonset.yaml'))
    def test_create_daemonset(self, daemonsetinfo):
        # 创建daemonset接口-正常
        base_url = datas.baseUrl + Testdaemonset.url_unify
        url = base_url + daemonsetinfo['request']['url'].replace('{namespace}', 'default')
        method = daemonsetinfo['request']['method']
        data = daemonsetinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        daemonsetname = req.json()['metadata']['name']
        globals()['daemonsetname'] = daemonsetname
        assert req.status_code == daemonsetinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.daemonset
    @pytest.mark.parametrize('daemonsetinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/daemonset/2_create_daemonset_exception.yaml'))
    def test_create_daemonset_exception(self, daemonsetinfo):
        # 创建daemonset接口-异常
        base_url = datas.baseUrl + Testdaemonset.url_unify
        url = base_url + daemonsetinfo['request']['url'].replace('{namespace}', 'default')
        method = daemonsetinfo['request']['method']
        data = daemonsetinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == daemonsetinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.daemonset
    @pytest.mark.parametrize('daemonsetinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/daemonset/3_get_specific_daemonset.yaml'))
    def test_get_specific_daemonset(self, daemonsetinfo):
        # 获取指定daemonset接口-正常
        base_url = datas.baseUrl + Testdaemonset.url_unify
        url = (base_url + daemonsetinfo['request']['url']
               .replace('{namespace}', 'default').replace('{daemonsetname}', globals()['daemonsetname']))
        method = daemonsetinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == daemonsetinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.daemonset
    @pytest.mark.parametrize('daemonsetinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/daemonset/3_get_specific_daemonset_exception.yaml'))
    def test_get_specific_daemonset_exception(self, daemonsetinfo):
        # 获取指定daemonset接口-异常
        base_url = datas.baseUrl + Testdaemonset.url_unify
        url = (base_url + daemonsetinfo['request']['url']
               .replace('{namespace}', 'default').replace('{daemonsetname}', globals()['daemonsetname']))
        method = daemonsetinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == daemonsetinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.daemonset
    @pytest.mark.parametrize('daemonsetinfo',
                             YamlUtil().read_testcase_yaml('v25.06/resourcemanage/daemonset/4_update_daemonset.yaml'))
    def test_update_daemonset(self, daemonsetinfo):
        # 更新指定daemonset接口
        base_url = datas.baseUrl + Testdaemonset.url_unify
        url = (base_url + daemonsetinfo['request']['url']
               .replace('{namespace}', 'default').replace('{daemonsetname}', globals()['daemonsetname']))
        method = daemonsetinfo['request']['method']
        data = daemonsetinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == daemonsetinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.daemonset
    @pytest.mark.parametrize('daemonsetinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/daemonset/4_update_daemonset_exception.yaml'))
    def test_update_daemonset_exception_exception(self, daemonsetinfo):
        # 更新指定daemonset接口-异常
        base_url = datas.baseUrl + Testdaemonset.url_unify
        url = (base_url + daemonsetinfo['request']['url']
               .replace('{namespace}', 'default').replace('{daemonsetname}', globals()['daemonsetname']))
        method = daemonsetinfo['request']['method']
        data = daemonsetinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == daemonsetinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.daemonset
    @pytest.mark.parametrize('daemonsetinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/daemonset/5_update_section_daemonset.yaml'))
    def test_update_section_daemonset(self, daemonsetinfo):
        # 部分更新指定daemonset的接口-正常
        base_url = datas.baseUrl + Testdaemonset.url_unify
        url = (base_url + daemonsetinfo['request']['url']
               .replace('{namespace}', 'default').replace('{daemonsetname}', globals()['daemonsetname']))
        method = daemonsetinfo['request']['method']
        data = daemonsetinfo['request']['data']
        header = daemonsetinfo['request']['headers']
        req = RequestUtil().send_request(method, url, headers=header, json=data)
        assert req.status_code == daemonsetinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.daemonset
    @pytest.mark.parametrize('daemonsetinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/daemonset/5_update_section_daemonset_exception.yaml'))
    def test_update_section_daemonset_exception(self, daemonsetinfo):
        # 部分更新指定daemonset的接口-异常
        base_url = datas.baseUrl + Testdaemonset.url_unify
        url = (base_url + daemonsetinfo['request']['url']
               .replace('{namespace}', 'default').replace('{daemonsetname}', globals()['daemonsetname']))
        method = daemonsetinfo['request']['method']
        data = daemonsetinfo['request']['data']
        header = daemonsetinfo['request']['headers']
        req = RequestUtil().send_request(method, url, headers=header, json=data)
        assert req.status_code == daemonsetinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.daemonset
    @pytest.mark.parametrize('daemonsetinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/daemonset/6_delete_specific_daemonset.yaml'))
    def test_delete_specific_daemonset(self, daemonsetinfo):
        # 删除daemonset接口-正常
        base_url = datas.baseUrl + Testdaemonset.url_unify
        url = (base_url + daemonsetinfo['request']['url']
               .replace('{namespace}', 'default').replace('{daemonsetname}', globals()['daemonsetname']))
        method = daemonsetinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == daemonsetinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.daemonset
    @pytest.mark.parametrize('daemonsetinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/daemonset/6_delete_specific_daemonset_exception.yaml'))
    def test_delete_specific_daemonset_exception(self, daemonsetinfo):
        # 删除daemonset接口-异常
        base_url = datas.baseUrl + Testdaemonset.url_unify
        url = (base_url + daemonsetinfo['request']['url']
               .replace('{namespace}', 'default').replace('{daemonsetname}', globals()['daemonsetname']))
        method = daemonsetinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == daemonsetinfo['validate'][0]['equals']['status_code']
